package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.DedicatedLineBean;
import com.bhs.sansonglogistics.bean.DedicatedLineDataBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 选择专线
 */
public class BottomSelectiveLineDialog extends Dialog implements View.OnClickListener, NetCallBack {

    private RecyclerView mRvList;

    private BaseActivity activity;
    private BaseFragment fragment;
    private BaseQuickAdapter<DedicatedLineBean, BaseViewHolder> mAdapter;
    private TextView tvTips;
    private int type;//1装车发货选专线 2.企业合作选专线
    private Button mBtnConfirm;

    public BottomSelectiveLineDialog(@NonNull BaseActivity context, int type) {
        super(context, R.style.MyBottomDialogStyle);
        this.activity = context;
        this.type = type;
    }

    public BottomSelectiveLineDialog(@NonNull BaseFragment context, int type) {
        super(context.getContext(), R.style.MyBottomDialogStyle);
        this.fragment = context;
        this.type = type;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_select_lien);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();

        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = MyUtils.getScreenHeight(getContext()) * 5 / 10;

        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {

        findViewById(R.id.iv_close).setOnClickListener(this);
        TextView mTvTitle = findViewById(R.id.tv_title);
        mRvList = findViewById(R.id.rv_list);

        mTvTitle.setText("选择专线");
        mAdapter = new BaseQuickAdapter<DedicatedLineBean, BaseViewHolder>(R.layout.item_special_line) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, DedicatedLineBean item) {
                if (type == 1) {
                    helper.setText(R.id.tv_refuse, String.format("%s-%s(共%s单)", item.getStart_city(), item.getEnd_city(), item.getOrder_count()));
                } else {
                    if (!TextUtils.isEmpty(item.getStart_city())) {
                        helper.setText(R.id.tv_refuse, String.format("%s-%s", item.getStart_city(), item.getEnd_city()));
                    } else {
                        helper.setText(R.id.tv_refuse, "暂不绑定");
                    }
                }
                helper.setImageResource(R.id.iv_refuse, item.isChecked() ? R.mipmap.ic_checked : R.mipmap.ic_unchecked);
            }
        };
        mRvList.setAdapter(mAdapter);
        Context context = activity != null ? activity : fragment.getContext();
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        if (type == 1) {
            activity.networkRequest(activity.netApi.getSpecialLineList(1, 10), this);
        } else {
            if (activity != null) {
                activity.networkRequest(activity.netApi.getSpecialList(1, 10), this);
            } else {
                fragment.networkRequest(fragment.netApi.getSpecialList(1, 10), this);
            }
        }
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty2, null));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (DedicatedLineBean datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                if (type == 1) {
                    myClickListener.onClick(String.valueOf(mAdapter.getData().get(position).getLine_id()),
                            mAdapter.getData().get(position).getStart_city() + "—" + mAdapter.getData().get(position).getEnd_city());
                    dismiss();
                }
                mAdapter.getData().get(position).setChecked(true);
                mAdapter.notifyDataSetChanged();

            }
        });
        mBtnConfirm = findViewById(R.id.btn_confirm);

        if (type == 2) {
            tvTips = findViewById(R.id.tv_tips);
            tvTips.setVisibility(View.VISIBLE);
            mBtnConfirm.setVisibility(View.VISIBLE);
            mBtnConfirm.setOnClickListener(this);
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        DedicatedLineDataBean dataBean = new Gson().fromJson(json, DedicatedLineDataBean.class);
        if (dataBean.isStatus()) {
            mAdapter.setNewData(dataBean.getData().getList());
            if (mAdapter.getData().size() > 0) {
                mAdapter.getData().get(0).setChecked(true);
                if (type == 2) {
                    mAdapter.getData().add(0,new DedicatedLineBean());
                }
                mAdapter.notifyDataSetChanged();
            }
        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private MyClickListener2 myClickListener;

    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }

    @Override
    public void onClick(View view) {
        dismiss();
        if (view.getId() == R.id.btn_confirm) {
            for (DedicatedLineBean datum : mAdapter.getData()) {
                if (datum.isChecked()) {
                    if (datum.getLine_id() > 0) {
                        myClickListener.onClick(String.valueOf(datum.getLine_id()), "1");
                    } else {
                        myClickListener.onClick("", "0");
                    }
                    break;
                }
            }
        }
    }
}
